<template>
  <div>
    <el-card>
      <!-- 查询条件 -->
      <el-form :model="param" label-width="80px" label-position="left">
        <el-row :gutter="20">
          <!-- 账号 -->
          <el-col :lg="8" :xl="6">
            <el-form-item label="账号">
              <el-input v-model="param.account"></el-input>
            </el-form-item>
          </el-col>
          <!-- 名称 -->
          <el-col :lg="8" :xl="6">
            <el-form-item label="名称">
              <el-input v-model="param.name"></el-input>
            </el-form-item>
          </el-col>
          <!-- 状态 -->
          <el-col :lg="8" :xl="6">
            <el-form-item label="状态">
              <el-select v-model="param.status" clearable>
                <el-option label="正常" value="0"></el-option>
                <el-option label="已禁用" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label-width="0px">
              <HButton type="search" @click="enquire">搜索</HButton>
              <HButton type="reset" @click="restForm">重置</HButton>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 按钮组 -->
      <el-row type="flex">
        <HButton type="add" @click="add">新增 </HButton>
      </el-row>
      <!-- 表格 -->
      <el-table class="mt-20" :data="tableData" border stripe>
        <el-table-column type="index" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="account" label="账号" align="center">
        </el-table-column>
        <el-table-column prop="name" label="名称" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="联系电话" align="center">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          align="center"
          :formatter="formatType"
        >
        </el-table-column>
        <el-table-column
          prop="roleNames"
          label="医生类型"
          align="center"
          :formatter="formatType1"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          header-align="center"
          align="center"
          width="200px"
        >
          <template slot-scope="scope">
            <HButton @click="edit(scope.row)" type="update" size="mini"
              >修改</HButton
            >
            <HButton
              @click="del(scope.row)"
              type="delete"
              size="mini"
              >{{scope.row.status == '0'?'禁用':'启用'}}</HButton
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="param.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="param.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <addDia ref="addDia" @transData="transData"></addDia>
  </div>
</template>

<script>
import addDia from "./components/user-add";
import { getUser, updateUserStatus } from "@/api/family_doctor/family_doctor";
export default {
  components: { addDia },
  data() {
    return {
      total: 0,
      tableData: [],
      param: {
        page: 1,
        limit: 10,
        account: "",
        name: "",
        status: "",
      },
      userparam: {
        id: "",
        status: 1,
      },
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    //搜索
    enquire() {
      this.param.page = 1;
      this.param.limit = 10;
      this.getTableData();
    },
    //重置
    restForm() {
      this.param.account = "";
      this.param.name = "";
      this.param.status = "";
    },
    getTableData() {
      getUser(this.param).then((res) => {
        if (res.data != null) {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    //增加
    add() {
      this.$refs.addDia.openDialog("1");
    },
    //编辑
    edit(row) {
      this.$refs.addDia.openDialog("2", row);
    },
    //删除
    async del(row) {
		var msg = "是否"+(row.status==0?'禁用？':'启用？');
      const confirmResult = await this.$confirm(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消操作");
      }
      this.userparam.id = row.id;
	  this.userparam.status = row.status==0?1:0;
	  
      updateUserStatus(this.userparam).then((res) => {
        if (res.code == "AA000000") {
          this.$message.success("操作成功！");
		  row.status =  this.userparam.status
        }
      });
    },
    //几页每条
    handleSizeChange(size) {
      this.param.limit = size;
      this.getTableData();
    },
    //第几页
    handleCurrentChange(page) {
      this.param.page = page;
      this.getTableData();
    },
    //供子组件调用函数
    transData() {
      this.getTableData();
    },
    //状态转换
    formatType(row, column) {
      return row.status == "0" ? "正常" : row.status == "1" ? "已禁用" : "";
    },
    formatType1(row, column) {
      return row.roleNames.toString()
    },
  },
};
</script>